home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_03 / cindric / inssort.c < prev   
Text File  |  1994-12-29  |  1KB  |  46 lines

  1. /*----------------------------*
  2.  * Special Insertion Sort     *
  3.  * (c) 1994, Blase B. Cindric *
  4.  * All rights reserved, and   *
  5.  *  jealously guarded         *
  6.  *----------------------------*/
  7.  
  8. void special_insertion_sort(
  9.  
  10.    unsigned *list,  /* array to be sorted */
  11.    int n,           /* no. of elements in array */
  12.    int num_sorted) /* no. of elements already in order */
  13.  
  14. {
  15.  
  16.     int j, k;
  17.     unsigned item_to_place;
  18.  
  19.     /*-------------------------------------------*
  20.      *  subscripts for sorted portion of array:  *
  21.      *     0 to (num_sorted - 1)                 *
  22.      *  subscripts for unsorted portion:         *
  23.      *     num_sorted to n                       *
  24.      *-------------------------------------------*/
  25.  
  26.     for (k = num_sorted; k < n; k++) {
  27.  
  28.                 /* move new item out of array */
  29.  
  30.         item_to_place = list[k];
  31.  
  32.                 /* copy all values larger than new item down one
  33.                      place in the array */
  34.  
  35.         for (j = k - 1; list[j] > item_to_place && j >= 0; j--)
  36.             list[j+1] = list[j];
  37.  
  38.                 /* place new item in its proper array position */
  39.  
  40.         list[j+1] = item_to_place;
  41.  
  42.     }  /* end of outer for loop */
  43.  
  44. }  /* end of special insertion sort */
  45.  
  46.